/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 
 * Xem nội dung các file trong thư mục hiện hành (lệnh dir)
 * Copy file/danh sách file (lệnh copy)
 * Xóa file/danh sách file (lệnh del)
 * Đổi tên file/thư mục  (lệnh rename)
 * Copy/Xóa thư mục (lệnh copydir - deldir)
 * Tạo tập tin văn bản (createtxt)
 * Chuyển thư mục hiện hành (cd)

 */
package bai_1;
import java.io.*;

/**
 *
 * @author PyyPyLyn
 */
public class Bai_1 {
    public static void main(String[] args) throws IOException
    {
        String help = "help";
        String dir = "dir";
        String copy = "copy";
        String delete = "delete";
        String rename = "rename";
        String createtextfile = "createtextfile";
        String cd = "cd";
        String exit = "exit";
        
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        
        System.out.println("Nhap help de xem huong dan cac lenh va exit de thoat chuong trinh.");
        String path = System.getProperty("user.dir");
        System.out.println(path+">");
        do {            
            String cmd = in.readLine();
            String arrcmd[] = cmd.split(" ");
            
            if(exit.compareTo(arrcmd[0]) == 0)
            {
                in.close();
                System.out.println("Da dong chuong trinh!");
                return;
            }
            else if (help.compareTo(arrcmd[0]) == 0)
            {
                System.out.println("Cac Cau Lenh:");
                System.out.println("copy: copy '<file/thumuc>' '<path>' | Copy file den duong dan path.");
                System.out.println("dir: dir | Xem noi dung ben trong thu muc hien hanh.");
                System.out.println("cd: cd '<path>' | Chuyen den duong dan path");
                System.out.println("del: delete '<file/thumuc>' | Delete file hoac thu muc trong thu muc hien hanh");
                System.out.println("createtextfile: createtextfile '<filename>' '<noi dung>' | Tao tap tin text.");
                System.out.println("rename: rename '<file/folder>' '<file/folder>' | Doi ten tap tin/thu muc.");
            }
            else if(dir.compareTo(arrcmd[0]) == 0)
            {
                //Xem noi dung thu muc hien hanh.
                dir(path);
            }
            else if(copy.compareTo(arrcmd[0]) == 0)
            {
                if(arrcmd.length == 3)
                {
                    copy(path,arrcmd[1],arrcmd[2]);
                }
                else
                {
                    System.out.println("Thieu hoac du tham so.");
                }
            }
            else if(delete.compareTo(arrcmd[0]) == 0)
            {
                if(arrcmd.length == 2)
                {
                    delete(path,arrcmd[1]);
                }
                else
                {
                    System.out.println("Khong du tham so.");
                }
            }
            else if(rename.compareTo(arrcmd[0]) == 0)
            {
                if(arrcmd.length == 3)
                {
                    rename(path,arrcmd[1],arrcmd[2]);
                }
                else
                {
                    System.out.println("Khong du tham so.");
                }
            }
            else if(createtextfile.compareTo(arrcmd[0]) == 0)
            {
                if(arrcmd.length == 3)
                {
                    createtextfile(path,arrcmd[1],arrcmd[2]);
                }
                else
                {
                    System.out.println("Khong du tham so.");
                }
            }
            else if(cd.compareTo(arrcmd[0]) == 0)
            {
                if(arrcmd.length >= 2)
                {
                    String pathdes = "";
                    for(int i = 1; i<arrcmd.length;i++)
                    {
                        pathdes = pathdes+ " " +arrcmd[i];
                    }
                    pathdes = pathdes.substring(2, pathdes.length()-1);                    
                    path = cd(path,pathdes);
                }
                else
                {
                    System.out.println("Khong du tham so.");
                }
            }
            else
            {
                System.out.println("Sai cu phap. Ban help de xem cu phap cac lenh.");
            }
            System.out.print(path+">");
        } while (true);
    }
    


// Xem noi dung thu muc hien hanh.
    static void dir(String path)throws IOException
    {
        File dir = new File(path);
        if(dir.isDirectory())
        {
            File[] list = dir.listFiles();
            for(File file : list)
            {
                System.out.println(file.getName());
            }
        }
    }
//------------------------------------

    
// Copy file/thu muc.
    public static void copy(String path, String pathname, String destinationpath ) throws IOException
    {
        File source = new File(pathname);
        File destination = new File(destinationpath);
        if(!source.exists())
        {      
            source = new File(path+"\\"+pathname);
            if(!source.exists())
            {
                System.out.println("Khong ton tai duong dan file/folder can sao chep.");
                return;
            }
        }
        if(!destination.isDirectory())
        {
            System.out.println("Khong ton tai duong dan dich nay.");
            return;
        }
        if(source.isDirectory())
        {
            copyDirectory(source, destination, null);
        }
        else if(source.isFile())
        {
            copyFile(source, destination);
        }        
    }
    
    public static void copyDirectory(File source, File destination, FileFilter filter) throws IOException
    {
        File nextDirectory = new File(destination, source.getName());
        if(!nextDirectory.exists() && !nextDirectory.mkdir())
        {
            Object[] filler = {nextDirectory.getAbsolutePath()};
            String messesage = "Copy that bai!";
            throw new IOException(messesage);
        }
        File[] files = source.listFiles();
        for(int n = 0; n < files.length; ++n)
        {
            if(filter == null || filter.accept(files[n]))
            {
                if(files[n].isDirectory())
                {
                    copyDirectory(files[n], nextDirectory, filter);
                }
                else 
                {
                    copyFile(files[n], nextDirectory);
                }
            }
        }
    } 
    public static void copyFile(File source, File destination) throws IOException 
    {
   
        if (destination.isDirectory()) 
        {
            destination = new File(destination, source.getName());
        }
        FileInputStream input = new FileInputStream(source);
       
        FileOutputStream output = new FileOutputStream(destination);
        try 
        {            
            byte[] buffer = new byte[1024];
            int byteRead = input.read(buffer);
            while(byteRead >= 0)
            {
                output.write(buffer, 0, byteRead);
                byteRead = input.read(buffer);
            }
        }
        catch(Exception e)
        {
            System.out.println("Co loi trong qua trinh sao chep tap tin. Sao chep that bai.");
        }
        finally 
        {
            input.close();
            output.close();
        }
    }

//-------------------------------------------

// delete file/thu muc.
    static void delete(String path, String pathDeleteItem)throws IOException
    {
        File f;
        try {
            f = new File(pathDeleteItem);
            if(f.isDirectory() || f.isFile())
            {
                if(f.isFile())
                {
                    dFile(f);
                }
                else if(f.isDirectory())
                {
                    dFolder(f);
                }
            }
            else
            {
                String newpath = path+"\\"+pathDeleteItem;
                f = new File(newpath);
                if(f.isDirectory() || f.isFile())
                {
                    if(f.isFile())
                    {
                        dFile(f);
                    }
                    else if(f.isDirectory())
                    {
                        dFolder(f);
                    }
                }
            }
        } catch (Exception e) {
            System.out.println("Xoa tap tin/thu muc that bai!");
        }
    }
    static void dFile(File file)throws IOException
    {
        boolean result = file.delete();    
        if(result == false)
            System.out.println("Khong xoa duoc tap tin " +file.getName()+ "!");
    }
    static void dFolder(File folder)throws IOException
    {
        File[] files = folder.listFiles();
        if(files != null)
        {
            for(File f : files)
            {
                if(f.isDirectory())
                {
                    dFolder(f);
                }
                else 
                {
                    dFile(f);
                }
            }
        }
        boolean result = folder.delete();
        if(result == false)
            System.out.println("Khong xoa duoc thu muc " +folder.getName()+ "!");
    }
//----------------------------------------------

    
// rename file/thu muc.
    static void rename(String path, String pathfile, String name)throws IOException
    {
        File f = new File(pathfile);
                
        if (f.isFile() || f.isDirectory())
        {
            f.renameTo(new File(f.getParent()+name));
        }        
        else
        {
            f = new File(path+"\\"+pathfile);
            if(f.isFile() || f.isDirectory())
            {
                try {
                    f.renameTo(new File(path+"\\"+name));
                    System.out.println("Doi ten thanh cong.");
                } catch (Exception e) {
                    System.out.println("Doi ten that bai.");
                }                
            }
            else
            {
                System.out.println("File khong ton tai");
            }            
        }
        
    }
//----------------------------------------------
    
    
// createtextfile
    static void createtextfile(String path, String name, String content) throws IOException
    {                    
        try 
        {            
            FileOutputStream fout = new FileOutputStream(path+"\\"+name);
            fout.flush();
            fout.close();
            System.out.println("Tao tap tin thanh cong.");
        } 
        catch (FileNotFoundException ex) 
        {
           System.out.println("Tao tap tin that bai.");
           return;
        }
        
        try 
        {            
            FileWriter fw = new FileWriter(path+"\\"+name);
            fw.write(content);
            fw.close();
            System.out.println("Ghi noi dung thanh cong.");
        } 
        catch (FileNotFoundException ex) 
        {
           System.out.println("Ghi noi dung that bai.");
        }
    }  
//-------------------------------------------

    
// cd
    static String cd(String path, String folder)throws IOException
    {
        if ("..\\".compareTo(folder) == 0)
        {
            path = (new File(path).getParent());
            
        }
        else if("\\".compareTo(folder) == 0)
        {
            String paths[] = path.split("\\");
            path = paths[0]+"\\";
        }
        else
        {
            File f = new File(folder);
            if (f.exists())
                path = path+"\\"+f.getPath();
            else
            {
                f = new File(path+"\\"+folder);
                if(f.isDirectory())
                {
                    path = path+"\\"+folder;
                }
                else
                {
                    System.out.println("Khong ton tai duong dan nay.");
                }
            }      
        }
        return path;
             
    }
//------------------------------------

}